package com.bitinstitute.parkapp.animals.types;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.bitinstitute.parkapp.R;
import com.bitinstitute.parkapp.helper.AlertDialogManager;
import com.bitinstitute.parkapp.helper.InternetConnectionDetector;
import com.bitinstitute.parkapp.helper.TabGroupActivity;
import com.bitinstitute.parkapp.helper.XMLParser;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TypesMenuActivity extends Activity {
	
	// Connection detector
	InternetConnectionDetector cd;
	
	// Alert dialog manager
	AlertDialogManager alert = new AlertDialogManager();
	
	// All static variables
	static final String URL = "http://watermark.wa.funpic.de/parkGuide/tiere/arten.xml";
	// XML node keys
	static final String KEY_ITEM = "item"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_NAME = "name";
	static final String KEY_SCI = "sci_name";
	static final String KEY_ORIGIN = "origin";
	static final String KEY_FOOD = "food";
	static final String KEY_SIZE = "size";
	static final String KEY_WEIGHT = "weight";
	static final String KEY_INCUBATION = "incubation";
	static final String KEY_EGGS = "eggs";
	static final String KEY_AGE = "age";
	static final String KEY_PLACE = "place";
	static final String KEY_HEADING1 = "heading1";
	static final String KEY_DESC1 = "description1";
	static final String KEY_HEADING2 = "heading2";
	static final String KEY_DESC2 = "description2";
	static final String KEY_HEADING3 = "heading3";
	static final String KEY_DESC3 = "description3";
	static final String KEY_THUMB_URL = "thumb_url";
	static final String KEY_SOUND = "sound";
	static final String KEY_VIDEO = "video";
	
	ListView list;
    TypesAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.types_main);
		
		// first: check for internet-connectivity
		cd = new InternetConnectionDetector(getApplicationContext());

		// Check if Internet present
		if (!cd.isConnectingToInternet()) {
			// Internet Connection is not present
			alert.showAlertDialog(getParent(),
					"Internet Connection Error",
					"Please connect to working Internet connection", false);
			// stop executing code by return
			return;
		}

		ArrayList<HashMap<String, String>> animalList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); // getting XML from URL
		Document doc = parser.getDomElement(xml); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		// looping through all animal nodes <item>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			map.put(KEY_SCI, parser.getValue(e, KEY_SCI));
			map.put(KEY_ORIGIN, parser.getValue(e, KEY_ORIGIN));
			map.put(KEY_FOOD, parser.getValue(e, KEY_FOOD));
			map.put(KEY_SIZE, parser.getValue(e, KEY_SIZE));
			map.put(KEY_WEIGHT, parser.getValue(e, KEY_WEIGHT));
			map.put(KEY_INCUBATION, parser.getValue(e, KEY_INCUBATION));
			map.put(KEY_EGGS, parser.getValue(e, KEY_EGGS));
			map.put(KEY_AGE, parser.getValue(e, KEY_AGE));
			map.put(KEY_PLACE, parser.getValue(e, KEY_PLACE));
			map.put(KEY_HEADING1, parser.getValue(e, KEY_HEADING1));
			map.put(KEY_DESC1, parser.getValue(e, KEY_DESC1));
			map.put(KEY_HEADING2, parser.getValue(e, KEY_HEADING2));
			map.put(KEY_DESC2, parser.getValue(e, KEY_DESC2));
			map.put(KEY_HEADING3, parser.getValue(e, KEY_HEADING3));
			map.put(KEY_DESC3, parser.getValue(e, KEY_DESC3));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
			map.put(KEY_SOUND, parser.getValue(e, KEY_SOUND));
			map.put(KEY_VIDEO, parser.getValue(e, KEY_VIDEO));

			// adding HashList to ArrayList
			animalList.add(map);
		}
		

		list = (ListView)findViewById(R.id.list);
		
		// Getting adapter by passing xml data ArrayList
        adapter = new TypesAdapter(this, animalList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
				String sci = ((TextView) view.findViewById(R.id.sci_name)).getText().toString();
				String origin = ((TextView) view.findViewById(R.id.origin)).getText().toString();
				String food = ((TextView) view.findViewById(R.id.food)).getText().toString();
				String size = ((TextView) view.findViewById(R.id.size)).getText().toString();
				String weight = ((TextView) view.findViewById(R.id.weight)).getText().toString();
				String incubation = ((TextView) view.findViewById(R.id.incubation)).getText().toString();
				String eggs = ((TextView) view.findViewById(R.id.eggs)).getText().toString();
				String age = ((TextView) view.findViewById(R.id.age)).getText().toString();
				String place = ((TextView) view.findViewById(R.id.place)).getText().toString();
				String heading1 = ((TextView) view.findViewById(R.id.heading1)).getText().toString();
				String desc1 = ((TextView) view.findViewById(R.id.desc1)).getText().toString();
				String heading2 = ((TextView) view.findViewById(R.id.heading2)).getText().toString();
				String desc2 = ((TextView) view.findViewById(R.id.desc2)).getText().toString();
				String heading3 = ((TextView) view.findViewById(R.id.heading3)).getText().toString();
				String desc3 = ((TextView) view.findViewById(R.id.desc3)).getText().toString();
				String thumb_url = ((TextView) view.findViewById(R.id.thumb_url)).getText().toString();
				String sound = ((TextView) view.findViewById(R.id.sound)).getText().toString();
				String video = ((TextView) view.findViewById(R.id.video)).getText().toString();
								
				// Starting new intent
				Intent in = new Intent(getApplicationContext(), TypesSingleMenuItemActivity.class);
				in.putExtra(KEY_NAME, name);
				in.putExtra(KEY_SCI, sci);
				in.putExtra(KEY_ORIGIN, origin);
				in.putExtra(KEY_FOOD, food);
				in.putExtra(KEY_SIZE, size);
				in.putExtra(KEY_WEIGHT, weight);
				in.putExtra(KEY_INCUBATION, incubation);
				in.putExtra(KEY_EGGS, eggs);
				in.putExtra(KEY_AGE, age);
				in.putExtra(KEY_PLACE, place);
				in.putExtra(KEY_HEADING1, heading1);
				in.putExtra(KEY_DESC1, desc1);
				in.putExtra(KEY_HEADING2, heading2);
				in.putExtra(KEY_DESC2, desc2);
				in.putExtra(KEY_HEADING3, heading3);
				in.putExtra(KEY_DESC3, desc3);	
				in.putExtra(KEY_THUMB_URL, thumb_url);
				in.putExtra(KEY_SOUND, sound);
				in.putExtra(KEY_VIDEO, video);

			
				TabGroupActivity parentActivity = (TabGroupActivity)getParent();
				parentActivity.startChildActivity("SingleMenu", in);
			}
		});		

        
       // Back Button 
       
	findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
		 
        public void onClick(View v) {

                dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

                dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

        }
	});
	
}
}